﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace CEFdemo
{
    public class ClientEncryption
    {
        private static RSAParameters _publicKey;

        public static void  SetPublicKey(string publicKeyXml)
        {
            using (var rsa = new RSACryptoServiceProvider(2048))
            {
                rsa.FromXmlString(publicKeyXml);
                _publicKey = rsa.ExportParameters(false);
            }
        }

        public static string Encrypt(string data)
        {
            using (var rsa = new RSACryptoServiceProvider(2048))
            {
                rsa.ImportParameters(_publicKey);
                var dataToEncrypt = Encoding.UTF8.GetBytes(data);
                var encryptedData = rsa.Encrypt(dataToEncrypt, false);
                return Convert.ToBase64String(encryptedData);
            }
        }
    }

}